package server

import (
	"github.com/gin-gonic/gin"
	"github.com/go-emix/emix-logrus"
	"service/common/doc"
	"strconv"
)

// 结尾不要加/，协议：域名
var NotifyHttp = ""

type Server struct {
	rou     *gin.Engine
	port    int
	errchan chan error
}

type Handlers func(r *gin.Engine)

func NewServer(port int, handlers Handlers) *Server {
	ser := &Server{}
	ser.port = port
	ser.errchan = make(chan error)
	ser.rou = gin.New()
	ser.rou.Use(gin.Recovery())
	handlers(ser.rou)
	emlogrus.Info("run server in " + strconv.Itoa(port))
	doc.CloseFile()
	return ser
}

func (r *Server) Run() {
	go func() {
		err := r.rou.Run(":" + strconv.Itoa(r.port))
		r.errchan <- err
	}()
	err := <-r.errchan
	panic(err)
}
